<?php
/*
	[UCenter Home] (C) 2007-2008 Comsenz Inc.
	$Id: profile.php 2009-11-3 $ 
*/

if(!defined('IN_UCHOME')) {
	exit('Access Denied');
}

include_once(S_ROOT.'./source/function_cp.php');
include_once(S_ROOT.'./site/libs/func_util.php');
include_once(S_ROOT.'./site/libs/func_model.php');
include_once(S_ROOT.'./source/function_bbcode.php');

if (empty($_GET['op'])) $_GET['op'] = 'index';

if ($_GET['op'] == 'new') {
	
	//实名认证
	ckrealname('share');
	
	//新用户见习
	cknewuser();
    

    if(empty($_SGLOBAL['supe_uid'])) {
        echo json_encode(retrieve_login_form());
        exit;
    }

    if(!submitcheck('mtagsubmit')) {
        //$fid = empty($_GET['fid'])?0:$_GET['fid'];

        ob_start();
        $cateselect = '<option value=0>   </option>\n';
        $subcatselect = "case \"0\":\n";
        $subcatselect .= "var sfOptions = new Array(";
        $subcatselect .= "\t\"\", \"0\");";
        $subcatselect .= "\t break;";
        $sfid = 0;
        foreach ($_SGLOBAL['profield'] as $p) {
            if (!$p['parentid'] && $p['profieldtype']) {
                $cateselect .= "<option value=\"".$p[fieldid]."\">".$p[title]."</option>\n"; 
                $subcatselect.= "case \"".$p[fieldid]."\":\n";
                $subcatselect.= "var sfOptions = new Array(";
                $subcatselect.= "\" \", \"0\",\n";
                foreach ($_SGLOBAL['profield'] as $k) {
                    if ($k['parentid'] == $p['fieldid']) {
                         $subcatselect .= "\"".$k['title']."\", \"".$k['fieldid']."\",\n"; 
                    }
                }
                $subcatselect = substr($subcatselect,0,-2) ."\t);\n";
                $subcatselect .= "\tbreak;\n";
            }
        }
        include_once template('site/tpl/create_profile');
       	$bgcontent = ob_get_contents();
        ob_end_clean();
        
        $r['html']=$bgcontent;
        echo json_encode($r);
        exit;
    } else {
        $sfnew = $_POST['sfnew'];
        $profid = $_POST['profid'];

        $tagname = $_POST['tagname'];
		
		if(strlen($tagname) < 2) {
			$r['err'] = "档案名不能小于1个中文字(2个英文字符)";
			echo json_encode($r);
			exit;
		}

		if(strlen($tagname) > 50) {
			$r['err'] = "档案名不能多于25个中文字(50个英文字符)";
			echo json_encode($r);
			exit;
		}

        $picurl = $_POST['pic'];
        $announcement = $_POST['announcement'];

        $r['err'] = 0;
        
        if (empty($sfnew)) {
           $r['err'] = "请选择正确的分类";
            echo json_encode($r);
            exit;
        } else {
            $fid = $sfnew;
        }
        $picurl = shtmlspecialchars(trim($picurl));
        if($picurl) {
            if(strlen($picurl) > 150) {
                $r['err'] = "图片链接请控制在150个字符之内";
                echo json_encode($r);
                exit;
            }	    
		    
            if(!preg_match("/^http\:\/\/.{5,150}\.(jpg|gif|png|jpeg)$                  /i", $picurl)) {
                $r['err'] = "无效的图片链接，我们支持的类型有(jpg, jpeg, gif, png), 链接请以http开头";
                echo json_encode($r);
                exit;
            }
    	}
        
        if(strlen($announcement) && (strlen($announcement) > 200)) {
            $r['err'] = "档案简介请保持在100个汉字之内";
            echo json_encode($r);
            exit;
        } elseif (strlen($announcement) < 2) {
            $r['err'] = "档案简介至少填一个汉字！";
            echo json_encode($r);
            exit; 
        }

        $query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('mtag')." WHERE tagtype='1' AND fieldid=".$fid." AND tagname='$tagname'");
        if($mtag = $_SGLOBAL['db']->fetch_array($query)) {
            //判断是否存在
            $r['err'] = "注意： 该档案已经存在！";
            echo json_encode($r);
            exit;
        }

        $mtag = create_profile('tagname', $tagname, $fid);
        if(empty($mtag)) {
            $r['err'] = "对不起， 不能创立档案！";
            echo json_encode($r);
            exit;
        } else {
		    $setarr = array();
            
            $setarr['pic'] = !empty($picurl) ? getpicurl($picurl, 150) : '';
		    $setarr['announcement'] = getstr($announcement, 200, 1, 1, 1, 1);
		    $setarr['moderator'] = $_SGLOBAL[supe_uid]; 
            updatetable('mtag', $setarr, array('tagid'=>$mtag['tagid']));

			//事件通知
			/*$fs = array();
			$fs['icon'] = 'prof';
			$fs['title_template'] = cplang('feed_prof_create');
			$fs['title_data'] = array(
					'tagname' => "<a href=\"site.php?ac=profile&profid=$mtag[tagid]\">$mtag[tagname]</a>"
				);
			$fs['body_template'] = '<b>{album}</b><br>';
			$fs['body_data'] = array(
				'album' => "<a href=\"site.php?ac=profile&profid=$mtag[tagid]\"><img class=\"summaryimg\" alt=\"$mtag[tagname]\" src=\"$picurl\"></a>");
			$fs['body_general'] = '';
			$fs['target_ids'] = "$tagid";

			feed_add($fs['icon'], $fs['title_template'], $fs['title_data'], $fs['body_template'], $fs['body_data'], $fs['body_general'], null, null, $fs['target_ids']);)*/

            $r['profid'] = $mtag['tagid'];

            $id = $_POST['id'];
            $picid = $_POST['picid'];

            $r['spotreferred'] = !empty($profid) && empty($picid) ? 1 : 0;

            if (isset($picid) && !empty($picid)) {
                $tagid = $mtag['tagid']; 
                $query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('profpic')." WHERE (profid='$tagid' AND picid='$picid')");
                if(!$exists = $_SGLOBAL['db']->fetch_array($query)) {
                    //
                    $psetarr = array(
                        'picid' => $picid,
                        'profid' => $tagid,
                        'uid' => $_SGLOBAL['supe_uid'],
                        'dateline' => $_SGLOBAL['timestamp'],
                        'postip' => getonlineip()
                    );
                    inserttable('profpic', $psetarr, 1);

                    $r['id'] = $id; 
                    $r['p'] = $p+1;
                    $r['spotreferred'] = $r['picreferred'] = 1;
                    echo json_encode($r);
                    exit;

                } else {
                    $r['err'] = "已经存在";
                    echo json_encode($r);
                    exit;
                }                
            } else if (!empty($profid)) {
                ob_start();
                include_once template('site/tpl/create_spot');
                $bgcontent = ob_get_contents();
                ob_end_clean();
                
                $r['html']=$bgcontent;
            }
            echo json_encode($r);
            exit;
        }
	}

} elseif($_GET['op'] == 'join') {
	$tagid = empty($_GET['profid'])?0:intval($_GET['profid']);
    //权限判断
    if(empty($_SGLOBAL['supe_uid'])) {
        //if($_SERVER['REQUEST_METHOD'] == 'GET') {
        //    ssetcookie('_refer', rawurlencode($_SERVER['REQUEST_URI']));
        //} else {
            ssetcookie('_refer', rawurlencode('site.php?ac=profile&op=join&profid='.$tagid));
        //}
        showmessage('to_login', 'do.php?ac='.$_SCONFIG['login_action']);
    }

	if(submitcheck('joinsubmit')) {
        
        //$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('mtag')." ");

		$mtag = profile_join('tagid', $tagid);
		if(empty($mtag)) {
			showmessage('mtag_join_error');
		} else {
			showmessage('prof_join_success', "site.php?ac=profile&profid=$mtag[tagid]", 0);
		}
	}

	include template('site/tpl/cp_prof');
} elseif($_GET['op'] == 'out') {

	$tagid = empty($_GET['profid'])?0:intval($_GET['profid']);
	$mtag = $tagid?getmtag($tagid):array();

	if(submitcheck('outsubmit')) {
		//对私密群组进行验证
		if(($mtag['joinperm'] > 0 || $mtag['viewperm'] > 0) && $mtag['grade'] == 9) {
			//验证是否还有主群组
			$count = $_SGLOBAL['db']->result($_SGLOBAL['db']->query("SELECT COUNT(*) FROM ".tname('tagspace')." WHERE tagid='$tagid' AND grade='9'"), 0);
			if($count < 2) {
				showmessage('failure_to_withdraw_from_group');
			}
		}
		if($mtag['status'] != -9) {
			profile_out($mtag, array($_SGLOBAL['supe_uid']));//退出
		}
		showmessage('do_success', "site.php?ac=profile&profid=$mtag[tagid]", 0);
	}

	include template('site/tpl/cp_prof');
} elseif ($_GET['op'] == 'list') {
	
    $page = empty($_GET['page'])?1:intval($_GET['page']);
    if($page<1) $page=1;

    // 导航栏
	$fid = empty($_GET['fid'])?0:intval($_GET['fid']);

    if (!$fid)
    {
	    showmessage('enter_the_space', 'index.php', 0);
    }

	if ($fid && !array_key_exists($fid, $_SGLOBAL['profield']))
	{
        showmessage('error_404');
	}

    $hotspotlist = getspots($fid);

    $sub_list = array();

    foreach($_SGLOBAL['profield'] as $p)
    {
        if ($p['parentid'] == $fid)
        {
            $sub_list[] = $p;
        }
    }
     
//    $hotproflist = getprofs($fid);

    $perpage = 20;
	$start = ($page-1)*$perpage;
	//检查开始数
	ckstart($start, $perpage);
    $theurl = "site.php?ac=profile&op=list";

	if($fid){
		foreach ($_SGLOBAL['profield'] as $value) {
			if ($value['fieldid'] == $fid && $value['parentid']) {
				$type = "and field.fieldid = $fid";
				$title1 = $value['title'];
				foreach ($_SGLOBAL['profield'] as $value2) {
					if ($value2['fieldid'] == $value['parentid']){
						$title2 = $value2['title'];
					}
				}
			} elseif ($value['fieldid'] == $fid && !$value['parentid']) {
				$title1 = $value['title'];
				foreach ($_SGLOBAL['profield'] as $value) {
					if ($value['parentid'] == $fid){
						$ids[] = $value['fieldid'];
					}
				}
				if($ids)
					$ids = implode(",", $ids);
				else
					$ids = '0';
				$type = "and field.fieldid in ($ids)";
				$theurl = "site.php?ac=profile&op=list&fid=$fid";
			}
		}
	}
	$list = $tagids = array();
	
	$count = $_SGLOBAL['db']->result($_SGLOBAL['db']->query("SELECT COUNT(*) FROM ".tname('mtag')." field WHERE 1=1 $type and tagtype = '1'"),0);
	//echo $count;
    if($count) {

        $sql = "SELECT field.* FROM ".tname('mtag')." field
			WHERE field.tagtype=1 $type LIMIT $start,$perpage"; 
        //echo $sql;
		$query = $_SGLOBAL['db']->query($sql);
		while ($value = $_SGLOBAL['db']->fetch_array($query)) {
			realname_set($value['uid'], $value['username']);
			if(empty($value['pic'])) {
				$value['pic'] = 'image/nologo.jpg';
			}
			if($value['grade']>-2) {
				$tagids[$value['tagid']] = $value['tagid'];//已经批准的群组
			}
			$list[] = $value;
		}
	}

	//分页
	$multi = multi($count, $perpage, $page, $theurl);
	
	realname_get();

    include_once template('site/tpl/profile_list');
    exit;
} elseif ($_GET['op'] == 'search') {
	//初始化
	$gets = $list = array();
	$multi = '';
		
    $gets['key'] = empty($_GET['key'])?'':(stripsearchkey($_GET['key'])?$_GET['key']:'');
    $gets['fieldid'] = empty($_GET['fieldid'])?0:intval($_GET['fieldid']);
    
    //开始搜索
    $wherearr = array();
    if($gets['fieldid']) {
        $wherearr[] = "main.fieldid = '$gets[fieldid]'";
    }
    //关键字
    if($inkey = stripsearchkey($gets['key'])) {
        if(preg_match("/( AND |\+|&|\s)/i", $inkey) && !preg_match("/( OR |\|)/i", $inkey)) {
            $keys = preg_replace("/( AND |&| )/i", "+", $inkey);
            $andor = ' AND ';
        } else {
            $keys = preg_replace("/( OR |\|)/i", "+", $inkey);
            $andor = ' OR ';
        }
        $is = array();
        foreach (explode('+', $keys) as $value) {
            if($value = trim($value)) {
                $is[] = "main.tagname LIKE '%$value%'";
            }
        }
        if($is) {
            $wherearr[] = '('.implode($andor, $is).')';
        }
    }
    if(empty($wherearr)) {
        showmessage('set_the_correct_search_content');
    }

    $query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('mtag')." main WHERE ".implode(' AND ', $wherearr)." AND main.tagtype =1 ORDER BY main.membernum DESC LIMIT 0, 100");//最多100条
    while ($value = $_SGLOBAL['db']->fetch_array($query)) {
        if(empty($value['pic'])) {
            $value['pic'] = 'image/nologo.jpg';
        }
        $value['spotcnt'] = getprofspotcnt($value['tagid']);
        $value['threadcount'] = getprofthreadcnt($value['tagid']);
        $value['fancount'] = getproffancnt($value['tagid']);
        $value['piccount'] = getprofpiccnt($value['tagid']);
        $list[] = $value;

		//更新最后操作时间
		//updatespacestatus('pay', 'search');
	}

	//群组
	$fieldids = array($gets['fieldid']=>' selected');
	realname_get();

	$_GET = shtmlspecialchars(sstripslashes($_GET));

	include_once template('site/tpl/search');
    exit;
} elseif ($_GET['op'] == 'index' || $_GET['op'] == 'pic' || $_GET['op'] == 'spot' || $_GET['op'] == 'thread' || $_GET['op'] == 'member') {
	
	@include_once(S_ROOT.'./data/data_profield.php');
	
	$id = empty($_GET['id'])?0:intval($_GET['id']);
	$profid = empty($_GET['profid'])?0:intval($_GET['profid']);

    $page = empty($_GET['page'])?1:intval($_GET['page']);
    if($page<1) $page=1;

    //指定的群组
	$mtag = getprof($profid);
    if (!$mtag) showmessage('enter_the_space', 'index.php', 0);

	$mtag[announce_bbcode] = html2bbcode($mtag[announcement]);

	if(empty($mtag['pic'])) {
		$mtag['pic'] = "image/nologo.jpg";
	} else {
		$mtag['pic'] = strlen($mtag['pic']) > 0 && $mtag['pic'] != "image/nologo.jpg" &&  file_exists($mtag['pic'].".160.jpg") ? $mtag['pic'].".160.jpg" : $mtag['pic'];
	}

    $nav_name = get_parent_name_by_fid($mtag['fieldid']);
	$li_name = get_li_name_by_fid($mtag['fieldid']);
        
    if ($li_name == 'star') { $li_chn_name = "明星"; } 
    elseif ($li_name == 'brand') { $li_chn_name = "品牌"; } 
    else { $li_chn_name = "时尚品"; } 

	if (!$nav_name) {
		showmessage('error_404');
		exit;
	}

    foreach ($_SGLOBAL['profield'] as $value) {
        if ($value['fieldid'] == $mtag['fieldid'] && $value['parentid']) {
            foreach ($_SGLOBAL['profield'] as $value2) {
                if ($value2['fieldid'] == $value['parentid']){
                    $title2 = $value2['title'];
                }
            }
        }
    }
    $spotcount = getprofspotcnt($profid);
    //$threadcount = getprofthreadcnt($profid);
    $fancount = getproffancnt($profid);
    $piccount = getprofpiccnt($profid);
    //群组首页
    $list = $starlist = $modlist = $memberlist = $checklist = array();
       
     //会员
	$query = $_SGLOBAL['db']->query("SELECT field.*, main.username, main.grade FROM ".tname('tagspace')." main 
					LEFT JOIN ".tname('spacefield')." field ON field.uid=main.uid 
					WHERE main.tagid='$profid'  ORDER BY main.grade DESC ");
	while ($value = $_SGLOBAL['db']->fetch_array($query)) {
		realname_set($value['uid'], $value['username']);
		$memberlist[] = $value;
	}

    realname_get();

    if ($_GET['op'] == 'index') {
		  
		//群组首页
		$list = $starlist = $modlist = $checklist = $spotlist= array();
			
		// 对每一个项做筛选

		$spots = getprofspotsbyfid($profid, 0, 0, 8);
        foreach($spots as $v) {
            if (is_array($v)) {
                for ($i = 0; $i < count($v); $i++)
                    $spotlist[] = $v[$i];
            }
        } 

		if ($mtag['pic'] == 'image/nologo.jpg')
			$edit_pic = '';
		else
			$edit_pic = $mtag['pic'];
	    
		//图片评论
		$perpage = 50;
		$start = ($page-1)*$perpage;
		
		//检查开始数
		ckstart($start, $perpage);

		$cid = empty($_GET['cid'])?0:intval($_GET['cid']);
		$csql = $cid?"cid='$cid' AND":'';
		$siteurl = getsiteurl();
		$list = array();
		$count = $_SGLOBAL['db']->result($_SGLOBAL['db']->query("SELECT COUNT(*) FROM ".tname('comment')." WHERE $csql id='$profid' AND idtype='profid'"),0);
		if($count) {
			$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('comment')." WHERE $csql id='$profid' AND idtype='profid' ORDER BY dateline  LIMIT $start,$perpage");
			while ($value = $_SGLOBAL['db']->fetch_array($query)) {
				realname_set($value['authorid'], $value['author']);
				$list[] = $value;
			}
		}

		 $sql = "SELECT count(*) FROM (SELECT DISTINCT * FROM ".tname('profpic')." WHERE profid ='$profid') AS cnt";
		$count = $_SGLOBAL['db']->result($_SGLOBAL['db']->query($sql), 0);
		$wheresql = "pp.profid='$profid'";

		if($count) {
			$query = $_SGLOBAL['db']->query("SELECT DISTINCT p.* FROM ".tname('pic')." p, ".tname('profpic')." pp WHERE p.picid=pp.picid AND $wheresql ORDER BY dateline DESC LIMIT 0,8");
			while ($value = $_SGLOBAL['db']->fetch_array($query)) {
				$value['pic'] = mkpicurl($value);
				$value['p'] = $start + $i;
				$plist[] = $value;
				$i++;
			}
		}

		//print_r($plist);
		//分页
		$multi = multi($count, $perpage, $page, "site.php?ac=profile&op=index&profid=$profid");

        $pics = getprofpics($profid, 0);    
		//print_r($pics);
		//print_r($catelist);
		include_once template('site/tpl/profile_home');
		exit;

	} elseif ($_GET['op'] == 'spot') {
        $perpage = 20;
        $fid = empty($_GET['fid'])?0:intval($_GET['fid']);
        $mpurl = "site.php?ac=profile&op=spot&profid=".$profid . ($fid?"&fid=".$fid:'');

        $spotcount = getprofspotcntbyfid($profid);

        $start = ($page-1)*$perpage;
        ckstart($start, $perpage);
       
        $total = 0;
        $ccnt = 0;
        if (count($spotcount)) {
            foreach($spotcount as $idx => $k) {
                if ($idx == $fid && $fid) $ccnt = $k; 
                $total += $k; 
            }
        }
        if (!$fid) $ccnt = $total;

        $spots = getprofspotsbyfid($profid, $fid, $start, $perpage);
        foreach($spots as $v) {
            for ($i = 0; $i < count($v); $i++)
                $spotlist[] = $v[$i];
        } 
        //print_r($spotlist) . "aaabbbb<br>";

        $multi = multi($ccnt, $perpage, $page, $mpurl);
		include_once template('site/tpl/profile_spot');
		exit;
	} elseif ($_GET['op'] == 'thread') {
			$perpage = 30;
			$start = ($page-1)*$perpage;
			//检查开始数
			ckstart($start, $perpage);
			
			$list = array();
			$count = 0;

			if($mtag['allowview']) {
				$count = $_SGLOBAL['db']->result($_SGLOBAL['db']->query("SELECT COUNT(*) FROM ".tname('thread')." main WHERE main.tagid='$profid' $wheresql"),0);
				if($count) {
					$query = $_SGLOBAL['db']->query("SELECT main.*, a.message FROM ".tname('thread')." main,  ".tname('post')." a
						WHERE main.tagid='$profid' $wheresql
						and main.tid = a.tid
						and a.isthread = '1'
						ORDER BY main.displayorder DESC, main.lastpost DESC 
						LIMIT $start,$perpage");
					while ($value = $_SGLOBAL['db']->fetch_array($query)) {
						realname_set($value['uid'], $value['username']);
						realname_set($value['lastauthorid'], $value['lastauthor']);
						$list[] = $value;
					}
				}
				//分页
				$multi = multi($count, $perpage, $page, "site.php?uid=ac=thread&op=list&profile=$tagid");
		
				realname_get();
			}

		include_once template('site/tpl/profile_thread');
		exit;
	} elseif ($_GET['op'] == 'member') {
		$perpage = 50;
		$start = ($page-1)*$perpage;
		
		//检查开始数
		ckstart($start, $perpage);
		
		//检索
		$wheresql = '';
		$_GET['key'] = stripsearchkey($_GET['key']);
		if($_GET['key']) {
			$wheresql = " AND main.username LIKE '%$_GET[key]%' ";
		}
		
		$list = $fuids = array();
		$count = 0;
		
		if($mtag['allowview']) {
			$count = $_SGLOBAL['db']->result($_SGLOBAL['db']->query("SELECT COUNT(*) FROM ".tname('tagspace')." main WHERE main.tagid='$profid' $wheresql"),0);
			if($count) {
				$query = $_SGLOBAL['db']->query("SELECT field.*, main.username, main.grade FROM ".tname('tagspace')." main 
					LEFT JOIN ".tname('spacefield')." field ON field.uid=main.uid 
					WHERE main.tagid='$profid' $wheresql ORDER BY main.grade DESC LIMIT $start,$perpage");
				while ($value = $_SGLOBAL['db']->fetch_array($query)) {
					//实名
					realname_set($value['uid'], $value['username']);
					
					$value['p'] = rawurlencode($value['resideprovince']);
					$value['c'] = rawurlencode($value['residecity']);
					$fuids[] = $value['uid'];
					$list[] = $value;
				}
			}
			
			//在线状态
			$ols = array();
			if($fuids) {
				$query = $_SGLOBAL['db']->query("SELECT uid, lastactivity FROM ".tname('session')." WHERE uid IN (".simplode($fuids).")");
				while ($value = $_SGLOBAL['db']->fetch_array($query)) {
					$ols[$value['uid']] = $value['lastactivity'];
				}
			}
	
			//分页
			$multi = multi($count, $perpage, $page, "space.php?uid=$space[uid]&do=mtag&tagid=$tagid&view=member");
			
			//实名
			realname_get();
		}

		include_once template('site/tpl/profile_member');
		exit;
	}
} elseif ($_GET['op'] == 'editpic') {
    $profid = empty($_GET['profid']) ? 0 : intval($_GET['profid']);

    if (!$_SGLOBAL['supe_uid']) {
        echo json_encode(retrieve_login_form());
        exit;
    }

    $query = $_SGLOBAL['db']->query("SELECT count(*) FROM ".tname('mtag')." WHERE tagtype='1' AND tagid=".$profid);
    if (!$_SGLOBAL['db']->result($query, 0)) {
        $r['err'] = "无效的操作";
        echo json_encode($r);
        exit; 
    }
    
    $res = save_prof_avatar($_FILES['userfile'], $profid);
    if ($res)
        $r['err'] = $res;
    else 
        $r['pic_title'] = $title;

    echo json_encode($r);
    exit; 
   
} elseif ($_GET['op'] == 'getoneprof') {
    $profid = empty($_GET['profid']) ? 0 : intval($_GET['profid']);
    
    $r['err'] = 0;
    $query = $_SGLOBAL['db']->query("SELECT count(*) FROM ".tname('mtag')." WHERE tagtype='1' AND tagid=".$profid);
    if (!$_SGLOBAL['db']->result($query, 0)) {
        $r['err'] = "oops";
        echo json_encode($r);
        exit; 
    }

    $r['prof'] = getprof($profid);
    json_encode($r);
    exit;

} elseif ($_GET['op'] == 'editannounce') {
    $profid = empty($_GET['profid']) ? 0 : intval($_GET['profid']);
    $announcement = $_POST['announcement'];
    
    $r['err'] = 0;
    $query = $_SGLOBAL['db']->query("SELECT count(*) FROM ".tname('mtag')." WHERE tagid=".$profid);
    if (!$_SGLOBAL['db']->result($query, 0)) {
        $r['err'] = "oops";
        echo json_encode($r);
        exit; 
    }

    if(strlen($announcement) && (strlen($announcement) > 200)) {
        $r['err'] = "档案简介请保持在100个汉字之内";
        echo json_encode($r);
        exit;
    } elseif (strlen($announcement) < 2) {
        $r['err'] = "档案简介至少填一个汉字！";
        echo json_encode($r);
        exit; 
    }
   
    $announcement = getstr($announcement, 200, 1, 1, 1, 1 );
    updatetable('mtag', array('announcement'=>$announcement), array('tagid'=>$profid));
    $r['announcement'] = $announcement;
    echo json_encode($r);
    exit; 
}  elseif ($_GET['op'] == 'show') {
	$page = empty($_GET['page'])?1:intval($_GET['page']);
	if (empty($_GET['view'])) 
		$_GET['view'] = 'me';

	if($_GET['view'] == 'me') {
			
		$perpage = 20;
		$start = ($page-1)*$perpage;
		
		//检查开始数
		ckstart($start, $perpage);

		$theurl = "site.php?ac=profile&op=show&view=mine";

		$list = $tagids = array();
		
		$count = $_SGLOBAL['db']->result($_SGLOBAL['db']->query("SELECT COUNT(*) FROM ".tname('tagspace')." main WHERE main.uid='$_SGLOBAL[supe_uid]'"),0);
		if($count) {
			$query = $_SGLOBAL['db']->query("SELECT main.*,field.* FROM ".tname('tagspace')." main 
			LEFT JOIN ".tname('mtag')." field ON field.tagid=main.tagid 
			WHERE main.uid='$_SGLOBAL[supe_uid]' AND field.tagtype=1 ORDER BY main.grade DESC LIMIT $start,$perpage");

			while ($value = $_SGLOBAL['db']->fetch_array($query)) {
				realname_set($value['uid'], $value['username']);
				if(empty($value['pic'])) {
					$value['pic'] = 'image/nologo.jpg';
				}
				if($value['grade']>-2) {
					$tagids[$value['tagid']] = $value['tagid'];//已经批准的群组
				}
				$list[] = $value;
			}
		}

		//分页
		$multi = multi($count, $perpage, $page, $theurl);
		
		//最新话题
		$threadlist = array();
		if($tagids) {
			$query = $_SGLOBAL['db']->query("SELECT main.*,field.tagname,field.membernum,field.fieldid FROM ".tname('thread')." main
				LEFT JOIN ".tname('mtag')." field ON field.tagid=main.tagid
				WHERE main.tagid IN (".simplode($tagids).") AND field.tagtype=1
				ORDER BY main.lastpost DESC
				LIMIT 0,10");
			while ($value = $_SGLOBAL['db']->fetch_array($query)) {
				realname_set($value['uid'], $value['username']);
				$threadlist[] = $value;
			}
		}

		realname_get();

		include_once template("site/tpl/uc_myprofile");
		exit;
	} elseif($_GET['view'] == 'hot') {
		$list = $pthreadlist = array();
		$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('mtag')." WHERE tagtype=1 ORDER BY spotcnt, membernum DESC LIMIT 0,2");
		
		while ($value = $_SGLOBAL['db']->fetch_array($query)) {
			if(empty($value['pic'])) {
				$value['pic'] = 'image/nologo.jpg';
			}
			$list[] = $value;
		}

		include_once template("site/tpl/uc_hotprofile");
		exit;
	} elseif($_GET['view'] == 'spot') {
		$proflist = $pthreadlist = array();
		$query = $_SGLOBAL['db']->query("SELECT tagid,tagname,membernum,pic FROM ".tname('mtag')." WHERE tagtype=1 ORDER BY spotcnt, membernum DESC LIMIT 0,2");
		while ($value = $_SGLOBAL['db']->fetch_array($query)) {
			if(empty($value['pic'])) {
				$value['pic'] = 'image/nologo.jpg';
			}
			$proflist[] = $value;
		}

		include_once template("site/tpl/uc_myspot");
		exit;
	}

}


?>
